<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="pwdForm"
      :rules="pwdFormRules"
      ref="pwdFormRef"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="revisePwd">修改密码</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updtaeUserPwdAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  name: 'UserPwd',
  data() {
    // 1.新密码和原密码不能相同
    const samePwd = (rule, value, callback) => {
      if (value === this.pwdForm.old_pwd) {
        return callback(new Error('新密码和原密码不能相同！'))
      }
      callback()
    }
    // 2. 确认密码要和和新密码相同
    const rePwd = (rule, value, callback) => {
      if (value !== this.pwdForm.new_pwd) {
        return callback(new Error('两次密码不一致！'))
      }
      callback()
    }

    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: '',
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
          {
            validator: samePwd,
            trigger: 'blur',
          },
        ],
        re_pwd: [
          { required: true, message: '请再次确认新密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur',
          },
          {
            validator: rePwd,
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    ...mapMutations(['updateToken', 'updateUserInfo']),
    // 修改密码
    revisePwd() {
      this.$refs.pwdFormRef.validate(async (valid) => {
        if (!valid) return false
        // 调接口
        const res = await updtaeUserPwdAPI(this.pwdForm)
        // 修改失败
        if (res.data.code !== 0) return this.$message.error(res.data.message)
        // 修改成功，被动退出
        // 1.提示用户
        this.$message.success(res.data.message)
        // 2. 清空输入框
        this.$refs.pwdFormRef.resetFields()
        // 3. 清空token
        this.updateToken('')
        // 4. 重置用户信息
        this.updateUserInfo({})
        // 5. 跳转到登录页，重新登录
        this.$router.replace({ name: 'Login' })
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.pwdFormRef.resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 500px;
}
</style>
